package com.csit.bean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 
 * @Description: 构造树的工具类
 * @Copyright: 福州骏华信息有限公司 (c)2012
 * @Created Date : 2012-8-17
 * @author longweier
 * @vesion 1.0
 */
public class Tree {
	// 排序方式
	public enum StateType{
		open, closed
	}
	private Object id;
	private String text;
	private String iconCls;
	private boolean checked;
	private StateType state;
	private Map<String,Object> attributes = new HashMap<String, Object>();
	private List<Tree> children = new ArrayList<Tree>();
	
	public static final String EMPTY = "[]";
	
	public Object getId() {
		return id;
	}
	public void setId(Object id) {
		this.id = id;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getIconCls() {
		return iconCls;
	}
	public void setIconCls(String iconCls) {
		this.iconCls = iconCls;
	}
	public boolean isChecked() {
		return checked;
	}
	public void setChecked(boolean checked) {
		this.checked = checked;
	}
	public StateType getState() {
		return state;
	}
	public void setState(StateType state) {
		this.state = state;
	}
	public Map<String, Object> getAttributes() {
		return attributes;
	}
	public void setAttributes(Map<String, Object> attributes) {
		this.attributes = attributes;
	}
	public List<Tree> getChildren() {
		return children;
	}
	public void setChildren(List<Tree> children) {
		this.children = children;
	}
	
	private JSONArray toJsonArray(List<Tree> children){
		JSONArray array = new JSONArray();
		for(Tree tree : children){
			array.put(toJsonObject(tree));
		}
		return array;
	}
	
	private JSONObject toJsonObject(Tree tree){
		JSONObject object = new JSONObject();
		try {
			object.put("id", tree.getId());
			object.put("text", tree.getText());
			object.put("iconCls", tree.getIconCls());
			object.put("checked", tree.isChecked());
			object.put("state", tree.getState());
			if(tree.getAttributes()!=null && !tree.getAttributes().isEmpty()){
				object.put("attributes", tree.getAttributes());
			}
			if(tree.getChildren().size()>0){
				object.put("children", toJsonArray(tree.getChildren()));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return object;
	}
	
	public String toJSON(){
		JSONObject object = toJsonObject(this);
		return "["+object.toString()+"]";
	}
	
	public String toString(){
		JSONObject object = toJsonObject(this);
		return object.toString();
	}
	
	public static String toJSON(List<Tree> list){
		StringBuilder json = new StringBuilder();
		if(list==null || list.size()==0){
			return EMPTY;
		}
		int index = 0;
		for(Tree tree : list){
			if(index>0) json.append(",");
			json.append(tree.toString());
			index++;
		}
		return "["+json.toString()+"]";
	}
}
